package cn.labelnet.android.roomdb.main

import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.DefaultItemAnimator
import android.support.v7.widget.GridLayoutManager
import android.util.Log
import android.widget.LinearLayout
import android.widget.Toast
import cn.labelnet.android.roomdb.R
import cn.labelnet.android.roomdb.base.config.MainData
import cn.labelnet.android.roomdb.base.data.DbHelper
import cn.labelnet.android.roomdb.base.data.tables.BookTable
import cn.labelnet.android.roomdb.base.data.tables.UserTable
import cn.labelnet.android.roomdb.main.adapter.MainAdapter
import cn.labelnet.android.roomdb.user.UserListActivity
import kotlinx.android.synthetic.main.activity_main.*
import java.text.SimpleDateFormat
import java.util.*

class MainActivity : AppCompatActivity(), MainAdapter.OnMainAdapterItemClick {

    private val mainAdapter = MainAdapter(this)
    private val configs = ArrayList<MainData>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initRecyclerView()
        initData()
    }

    private fun initData() {
        configs.add(MainData(1, "e.g 用户与书籍", getDrawable(R.drawable.ic_library_books_blue_400_24dp)))
        configs.add(MainData(2, "other", getDrawable(R.mipmap.ic_launcher_round)))
        mainAdapter.addDatas(configs)
    }

    /**
     * 初始化 recycler view
     */
    private fun initRecyclerView() {
        val gridManager = GridLayoutManager(this, 2)
        gridManager.orientation = LinearLayout.VERTICAL
        rvBaseData.layoutManager = gridManager
        rvBaseData.setHasFixedSize(true)
        rvBaseData.itemAnimator = DefaultItemAnimator()
        rvBaseData.adapter = mainAdapter
    }

    override fun onItemClick(type: Int) {
        when (type) {
            1 -> movePage(UserListActivity::class.java)
//            2 -> movePage(BookActivity::class.java)
            else -> Toast.makeText(this, "type: $type", Toast.LENGTH_LONG).show()
        }
    }

    private fun movePage(cls: Class<*>) {
        startActivity(Intent(this, cls))
    }

    private fun testTable(type: Int) {
        val user = UserTable(name = "yuan", phone = "15000000")

        Thread(Runnable {
            if (type == 1) {
                DbHelper.INSTANCE!!.getUserDao().insertUser(user)
            } else {
                val book = BookTable(name = "老人与海", author = "海明威", userId = 1)
                DbHelper.INSTANCE!!.getBookDao().insertBook(book)
            }
            Log.v("insert", "insert $type")
        }).start()

        Thread(Runnable {
            val selectUsers = DbHelper.INSTANCE!!.getUserDao().selectUsers()
            Log.v("select", selectUsers!!.toString())
            val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
            selectUsers.forEach { userTable -> Log.d("Main", "createDate : ${format.format(userTable.createDate)}") }
        }).start()
    }
}
